#include "cln_thread.h"
#include "clnt_socks.h"
#include "err_handle.h"
#include <string.h>
#include <unistd.h>
#include <stdio.h>

#define BUF_SIZE 1024
#define MAX_CLNT 256
#define NAME_SIZE 128

void *cln_thread(void *arg)
{
    int sock_cln = *((int *)arg);
    int read_len = 0;
    char buf[BUF_SIZE] = {0};
    while (1)
    {
        memset(buf, 0, BUF_SIZE);
        read_len = read(sock_cln, buf, BUF_SIZE);
        if (read_len == -1)
        {
            error_handling("read() error.");
        }
        else if (read_len > 0) //主要功能区
        {
            if (buf[0] == '@')
            {
                char *mbuf, *sp_msg;
                mbuf = buf;
                char *recver_name;
                char *delete_at, *at;
                at = strtok_r(mbuf, "@", &delete_at);
                recver_name = strtok_r(at, " ", &sp_msg);
                one_to_one(recver_name, sp_msg, sock_cln);
            }
            else
            {
                send_msg_to_all(buf, read_len);
            }
        }
        else if (read_len == 0)
        {
            break;
        }
    }
    return NULL;
}
